<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class TradeController extends CommonController {
	protected $tb_order = 'order';
	protected $tb_user = 'user';
	protected $tb_pay = 'payment' ;//支付方式表
	protected $tb_store = 'store' ;
	protected $tb_g_order = 'order_goods';
	
	/*
	*订单列表
	*/
    public function index(){
		
		if (I('get.order_id') && I('get.atc')=="order_info") {
			$this->order_info();
		} else {
			if (!$_GET['p']) {
				$page = 1 ;
			} else {
				$page = $_GET['p'] ;
			}
			//搜索
			unset($str);
			unset($arr);
			
			if ($_GET['select']=='s_order') {
				//搜索
				//订单编号
				if ($_GET ['order_sn']>0) {				
					$arr_data ['order_sn']  = $_GET ['order_sn'] ;
					$str1 = ' and b.order_sn=%d';
					$da_str1 = ','.$arr_data ['order_sn'] ;				
				} else {
					$str1 = '';
					$da_str1 = '' ;	
				}
				//订单状态
				if ($_GET ['order_state']) {
					if ($_GET ['order_state']=='1') {
						$arr_data ['order_state']  = 0 ;
					} else {
						$arr_data ['order_state']  = $_GET ['order_state'] ;
					}
					
					$str2  = ' and b.order_state=%d';
					$da_str2 = ','.$arr_data ['order_state'] ;
				} else {
					$str2  = '';
					$da_str2 = '' ;
				}
				//买家
				if ($_GET ['buyer_name']) {								
					$arr_data ['buyer_name']  = $_GET ['buyer_name'] ;
					$str3  = " and b.buyer_name='%s'";
					$da_str3 = ",".htmlentities($arr_data['buyer_name']);
				} else {
					$str3  = '' ;
					$da_str3 = '' ;
				}
				//下单时间
				$_GET ['start_time'] && $_GET ['start_time'] = strtotime($_GET ['start_time']) ;
				$_GET ['end_time'] && $_GET ['end_time'] = strtotime($_GET ['end_time']) ;
				if (!$_GET ['end_time'] && $_GET ['start_time']) {
						$_GET ['end_time'] = time();
					}
				//dump($_GET);die;
				if ($_GET ['start_time'] && $_GET ['start_time'] < $_GET ['end_time']) {	
					
					$str4  = ' and add_time>=%d and add_time<=%d';
					$da_str4 = ','.$_GET ['start_time'].','.$_GET ['end_time'] ;
				} else if ($_GET ['end_time'] && !$_GET ['start_time']) {								
					$this->error('请输入下单开始时间');
				} else if ($_GET['end_time'] && $_GET ['start_time'] && $_GET ['start_time'] > $_GET ['end_time']) {								
					$this->error('下单 开始时间 不能大于 结束时间');
				} else {
					$str4  = '' ;
					$da_str4 = '' ;
				}
				
				if ($_GET ['st_name']) {								
					//店铺
					$st_list = _getOne($this->tb_store,array('store_name'=>$_GET ['st_name']));
					$str5  = ' and b.store_id=%d';
					$da_str5 = ','.$st_list['store_id'] ;
				} else {
					$str5  = '' ;
					$da_str5 = '' ;
				}
							
				$str = "b.order_id>%d".$str1.$str2.$str3.$str4.$str5;
				$da_str = "0".$da_str1.$da_str2.$da_str3.$da_str4.$da_str5 ;
				
				$arr = explode(',',$da_str);
			} else {
				$str = "b.order_id>%d" ;
				$arr = array(0) ;
			}
			
			$num = 10 ;
			$table ='__ORDER__ AS b';

			$order=' b.order_id desc' ;	
			$join['join1'] = "LEFT JOIN __ORDER_GOODS__ AS bg ON bg.order_id=b.order_id";
			$field='b.*,bg.goods_id,bg.goods_name,bg.goods_price,bg.goods_num,b.shipping_fee' ;
			//分页
			$show = PageJoin($table, $str, $arr, $num, $join, $field);
				
			$list = _RowsJoin($table, $join, $str, $arr, $field, $order, $page, $num) ;
			
			foreach ($list as $key=>$vv) {
				unset($user);
				$user = _getOne($this->tb_user, 'id=%d', array($vv['buyer_id'])) ;
				$list [$key] ['unser_name'] = $user ['username'] ;
				//支付方式
				$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($vv['payment_code'])) ;
				$list [$key] ['pay_name'] = $pay_code ['payment_name'] ;
			}
			//dump($list);die;		
			$this->assign('list',$list) ;
			$this->assign('page',$show) ;
			$this->display();				
		}
	
    }
	
	/*
	*查看订单
	*/
	public function order_info() {
		if (I('get.order_id') && I('get.atc')=="order_info") {

			unset($str);
			unset($arr);			
			$str = "order_id=%d" ;
			$arr = array(I('get.order_id')) ;			
			$order = M($this->tb_order)->where($str,$arr)->find();
			
			$order['goods_list'] = M($this->tb_g_order)->where($str,$arr)->select();
			
			$pay_code = _getOne($this->tb_pay, 'payment_code="%s"', array($order['payment_code'])) ;
			$order ['pay_name'] = $pay_code ['payment_name'] ;
			//dump($order);die;
			$this->assign('order',$order);
			$this->display('order_info');			
		}
	}
	
	/*
	*订单操作
	*/	
	public function order_off() {
		
		if (IS_POST) {
			if (I('post.type')=='order_off') {
				if (I('post.state')) {
					unset($str);
					unset($arr);
					unset($data);
					$str = 'order_id=%d' ;
					$arr = array(I('post.id')) ;
					$data ['order_state'] = I('post.state') - 1;
					$edit = _getEdit($this->tb_order, $str ,$arr ,$data) ;
					if (I('post.state')=='21') {
						$edit?$msg = '确定收到货款成功！' : $msg = '确定收到货款失败！';
					} else {
						$edit?$msg = '取消成功！' : $msg = '取消失败！';
					}
					
				} else {
					$msg = '非法操作！' ;
				}

			} else {
			   $msg = '非法操作！' ;
			}
			
			$this->ajaxReturn(array('info'=>$msg));
		}
		$this->ajaxReturn(array('info'=>'非法操作！'));
	}

}